import random as rd
from courseSchedule.GlobalData import global_var


def generate_schedules(courses):
    schedules = []
    for course in courses:
        week_period = course["period"] // 2
        # 16周不能上完的分段生成课程时间
        while True:
            schedules.append(Schedule(
                course["course_id"],
                course["course_volume"],
                week_period if week_period < 16 else 16,
                course["teacher_id"]
            ))
            if week_period <= 16:
                break
            week_period -= 16
    return schedules


class Schedule:
    def __init__(self, course_id, volume, week_period, teacher_id):
        self.course_id = course_id
        self.volume = volume
        self.week_period = week_period
        self.teacher_id = teacher_id

        self.room_id = self.time = self.day = self.first_week = 0
        self.candidate_rooms = []

    def init_random(self):
        # 容量足够的作为候选教室
        self.candidate_rooms = [room["room_id"] for room in global_var["rooms"] if room["capacity"] > self.volume]
        self.room_id = self.candidate_rooms[rd.randint(0, len(self.candidate_rooms) - 1)]
        self.time = rd.randint(1, 6)
        self.day = rd.randint(1, 5)
        self.first_week = rd.randint(1, 17 - self.week_period)
